const express = require('express')
const mysql2 = require('mysql2')
const app = express()

app.get('/', async (req, res) => {
  // 创建连接池
  const config = getDBConfig()
  const promisePool = mysql2.createPool(config).promise()
  const user = await promisePool.query('select * from students')
  res.send({
    code: 200,
    msg: '请求成功！',
    data: user[0]
  })
})
app.listen(3000, () => {
  console.log('启动成功！')
})
const getDBConfig = () => ({
  host: '127.0.0.1',
  port: 3306,
  user: 'root',
  password: 'root',
  // 数据库名
  database: 'test',
  // 创建连接池数量
  connectionLimit: 1
})